<template>
  <div class="container">
    <!-- 顶部导航 -->
    <div class="nav-list">
     <Breadcrumb separator=">">
        <BreadcrumbItem to="/content">首页</BreadcrumbItem>
        <BreadcrumbItem to="/know-how/Coursegroup">云课堂</BreadcrumbItem>
        <BreadcrumbItem>{{currentChapter.title}}</BreadcrumbItem>
    </Breadcrumb>
    </div>
    <div class="container-box">
      <!-- 视频标题及介绍 -->
    <div class="video-window">
        <video id="myvideo" controls="controls" class="video"
             height="700px;"
             :src="videoSrc" :poster="videoImg"
             @play="onPlayerPlay($event)"
             @click="videoclick"
             @ended="onPlayerEnded($event)"
             @waiting="onPlayerWaiting($event)"
             @statechanged="playerStateChanged($event)">
             Your browser does not support the video tag.
       </video>
       <div class="video-intro">
          <div class="video-summary">
            <span class="video-title" size="3"><strong>{{currentChapter.title}}</strong></span>
            <span class="video-view"><Icon type="eye" size="15" color="lightskyblue"></Icon>{{currentChapter.views}} 人浏览</span>
            <div style="clear:both;"></div>
          </div>
      </div>
    </div>
    <div class="content-box">
       <Tabs value="name1">
         <TabPane label="视频概述" name="name1">
              <div class="chapterList">
               <div class="author-info">
                  <p class="author-intro"><strong>作者简介</strong></p>
                  <Icon type="person" size="80" class="avatar" color="lightskyblue"></Icon>
                  <p class="author">{{courseDetail.lecturerName}}</p>
                  <span class="lifetime">暂无简介</span>
                  <img src="../../assets/author.png" alt="" style="position:absolute;height:200px;width:200px;margin-top:-180px;margin-left:75%;">
               </div>
              <div class="course-box">
                <span class="course-intro">概述</span>
                <span class="intro">{{courseDetail.profile}}</span>
            </div>
          </div>
        </TabPane>
        <TabPane label="视频介绍" name="name2">
          <!-- 课程介绍 -->
          <Collapse v-model="value1" style="margin-top:-18px;width:70vw;">
             <Panel name="1">
               {{courseDetail.categoryName}}
                 <p slot="content" v-for="(chapter, index) in courseDetail.chapters" v-bind:key="index">
                  <font style="padding-left:30px;" :color="chapter.id == currentChapter.id ? '#333':''" :size="chapter.id == currentChapter.id ? 3:''">{{index + 1}}.</font>
                  <a v-on:click="playVideo(chapter)">
                    <font :color="chapter.id == currentChapter.id ? 'lightskyblue':''" :size="chapter.id == currentChapter.id ? 3:''" style="line-height:30px;font-size:16px;">{{chapter.title}}</font>
                  </a>
               </p>
            </Panel>
          </Collapse>
        </TabPane>
        <TabPane label="用户评论" name="name3">
         <!-- 评论区 -->
         <div class="discuss-list">
           <ul style="margin:0;padding:0;list-style:none;">
           <li v-for="(comment, index) in commentList" v-bind:key="index" class="discuss-content">
              <div class="user-image">
                 <Icon type="person" size="60" style="paddding-top:20px;margin-bottom:30px;"></Icon>
              </div>
              <section class="discuss-info">
                <div class="list-comment-main">{{comment.comment}}</div>
                <div class="list-comment-foot">
                 <span class="comment-user">{{comment.userName}}<span style="color:#999;padding-left:5px;">发表于</span></span>
                 <span class="comment-time">{{comment.gmtCreated}}</span>
              </div>
             </section>
          </li>
        </ul>
      </div>
      <div class="list-comment" style="margin:0 auto;">
       <!-- 分页 -->
        <div class="page">
            <Row class="code-row-bg">
              <Col>
              <Page show-total :total="total" :page-size="pageSize" @on-change="pageComment"></Page>
              </Col>
            </Row>
        </div>
        <div class="discuss">
          <row class="discuss-box">
            <i-col span="1">
              <Icon type="person" size="45" style="padding-left:3vw;"></Icon>
            </i-col>
            <i-col span="10">
              <div style="width:50vw;height:100px;margin-left:4vw;">
                <i-input v-model="comment" type="textarea" :autosize="{minRows: 3,maxRows: 8}" placeholder="扯淡、吐槽、表扬、鼓励……想说啥就说啥！"></i-input>
              </div>
            </i-col>
            <i-col span="20" offset="1">
              <div style="margin-top:-20px;width:60vw;margin-left:53%">
                <i-button type="success" size="large" @click="submitComment">发表评论</i-button>
              </div>
            </i-col>
          </row>
        </div>
       </div>
      </TabPane>
    </Tabs>
    </div>
    </div>
  </div>
</template>
<script>
import CourseAPI from "@/api/knowledge/CourseAPI";
import LearningRecordAPI from "@/api/knowledge/LearningRecordAPI";
import CommentAPI from "@/api/knowledge/CommentAPI";
import { videoPlayer } from "vue-video-player";

// require styles
import "video.js/dist/video-js.css";
export default {
  data() {
    return {
      courseDetail: "",
      currentChapter: "",
      pageNum: 1,
      pageSize: 5,
      total: 0,
      value1: '1',
      videoSrc:'',
      videoImg:'',
      playerOptions: {
        start: 0,
        autoplay: false,
        volume: 0.6,
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        playsinline: false,
        playbackRates: [0.7, 1.0, 1.5, 2.0],
        sources: [
          {
            type: "video/mp4",
            src: ""
          }
        ],
        poster: "",
        notSupportedMessage: '此视频暂无法播放，请稍后再试',
        //允许覆盖Video.js无法播放媒体源时显示的默认信息。
      },
      comment: "",
      commentList: ""
    };
  },
  components: {
    videoPlayer
  },
  mounted() {
    this.fetchCourseDetail();
    setTimeout(() => {
      this.playerOptions.muted = false;
    }, 100);
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player;
    }
  },
  methods: {
    fetchCourseDetail() {
      let courseId = this.$route.params.courseId;
      CourseAPI.fetchCourseDetail(
        courseId,
        (resp)=> {
          this.courseDetail = resp;
          if (resp.chapters.length == 0){
           this.$Message.info({
              content: "本课程没有章节内容,请返回上一页....",
              duration: 2,
            });
          } else {
            this.currentChapter = resp.chapters[0]; 
            // this.playerOptions.poster = resp.poster;
            // this.playerOptions.sources[0].src = resp.chapters[0].materialAddr;
            this.videoSrc=resp.chapters[0].materialAddr;
            this.videoImg = resp.poster;
            this.playerOptions.poster = resp.poster;
            this.videoImg = resp.poster;
            this.playerOptions.sources[0].src = resp.chapters[0].materialAddr;
            this.videoSrc=resp.chapters[0].materialAddr;
            this.fetchCommentList();
          }
        },
        (error)=> {
          this.$Message.error("操作失败【" + error + "】");
        }
      );
    },
    // listen event  onPlayerPlay onPlayerEnded onPlayerWaiting playerStateChanged
    onPlayerPlay(player) {
      let _this = this;
       if(this.currentChapter.id==undefined){
        return
      }
      LearningRecordAPI.saveLearningRecord(this.currentChapter.id, function(resp) {
        _this.currentChapter.views += 1;
      }, function(error) {
        _this.$Message.error("操作失败【" + error + "】");
      });
    },
    onPlayerEnded(player) {
      // let _this = this;
      LearningRecordAPI.updateLearningProgress(
        this.currentChapter.id,
        function(resp) {},
        function(error) {
          this.$Message.error("操作失败【" + error + "】");
        }
      );
    },
    playVideo(chapter) {
      this.currentChapter = chapter;
      this.playerOptions.poster = this.courseDetail.poster;
      this.playerOptions.sources[0].src = chapter.materialAddr;
      this.videoImg = this.courseDetail.poster;
      this.videoSrc = chapter.materialAddr;
      this.fetchCommentList();
 },
    playerStateChanged(playerCurrentState) {},
    onPlayerWaiting(player) {},
    videoclick(){
    var video = document.getElementById("myvideo");
    this.controls = true;
        if(video.paused) {
          video.play()
        }else{
          video.pause()
        }
    },
    submitComment() {
      if (this.comment === "") {
        this.$Message.warning("说点儿啥呗！");
        return;
      }
      let courseid=this.currentChapter.courseId;
      if(courseid==undefined){
        this.$Message.warning("暂无视频可评论！");
        return;
      }
      let params = [courseid, this.comment];
      CommentAPI.createComment(
        params,
        (resp)=> {
         if (resp.code != "0000") {
            // 后端报错
            this.$Message.error(resp.message);
          } else {
            this.$Message.success("成功发布评论！");
            this.comment = "";
          }
          this.fetchCommentList();
        },
        (error)=> {
          this.$Message.error("操作失败【" + error + "】");
        }
      );
    },
    fetchCommentList() {
      let params = [this.currentChapter.courseId, this.pageNum, this.pageSize];
      CommentAPI.fetchCommentList(
        params,
        (resp)=> {
          this.pageNum = resp.pageNum;
          this.pageSize = resp.pageSize;
          this.total = resp.total;
          this.commentList = resp.commentList;
        },
        (error)=> {
          this.$Message.error("操作失败【" + error + "】");
        }
      );
    },
    pageComment(current) {
      this.pageNum = current;
      this.fetchCommentList();
    }
  }
};
</script>
<style scoped>
.container {
  width: 100vw;
  height: auto;
  box-sizing: content-box;
  overflow-x: hidden;
  background-color: #f4f4f4;
  margin-bottom: 50px;
}
.container-box{
  width: 70vw;
  height: auto;
  margin: 0 auto;
}
/* 顶部导航 */
.nav-list{
  width:70vw;
  margin:0 auto;
  margin-top:70px;
}
/* 视频窗口 */
.video-window{
 width:70vw;
 margin:0 auto;
 /* padding-top:10px; */
}
/* 播放窗口 */
.video{
 width:70vw;
 margin:0 auto;
}
/* 视频简介 */
.video-intro{
 width:70vw;
 background-color:#fff;
 border-bottom: 1px dashed #e4e4e4;
}
.video-summary{
 width:70vw;
 height:40px;
 line-height: 40px;
}
.video-title{
  font-size: 1.14rem;
  padding-left: 10px;
}
.video-view{
  float: right;
  padding-right: 10px;
}
/* 视频概述 */
.author-info{
 width:70vw;
 height:auto;
 line-height:30px;
 margin-top:10px;
 position: relative;
}
.author-intro{
  font-size: 1rem;
  padding-left: 20px;
  padding-top: 5px;
}
.author{
  font-size: 1rem;
  padding-left: 25px;
  padding-top: -20px;
}
/* 用户头像 */
.avatar{
  border-radius: 100%;
  margin: 5px 0px 0px 20px;
  padding: 0px 5px 0px 5px;
}
.lifetime{
  position: absolute;
  text-align: center;
  margin-top: -90px;
  color:#999;
  padding-left: 40%;
}
/* 视频简介 */
.course-box{
  width: 70vw;
  position: relative;
  margin-top: 30px;
}
.course-intro{
  display: inline-block;
  width: 10 vw;
  /* 字间距 */
  letter-spacing: 20px;
  font-size: 1rem;
  line-height: 10px;
  padding-left: 25px;
  position: absolute;
  padding-top: 10px;
  color: #999;
}
.intro{
  display: inline-block;
  width: 55vw;
  position: relative;
  word-wrap: break-word;
  word-break: normal;
  color: #999;
  margin-left: 8vw;
  padding: 5px 10px 10px 5px;
}
/* 视频窗口 */
.mr-row {
  width: 70vw;
  height: 400px;
  margin: 0 auto;
}
/* 视频介绍及用户评论 */
.content-box {
  width: 70vw;
  margin: 0 auto;
  overflow: hidden;
  background-color: #fff;
}
/* 视频章节列表 */
.Chapter-list{
 width: 70vw;
 position: relative;
 padding-left: 20px;
 padding-top: 20px;
}
.Chapter-list li{
 display: block;
 width: 70vw;
 height: 30px;
 line-height: 30px;
}
/* 用户评论区 */
.comment {
  width: 70vw;
  padding-top: -10px;
}
.discuss-list{
  width: 70vw;
  height: auto;
  margin: 0 auto;
  position: relative;
  margin-top: -17px;
}
/* 评论人 */
.user-image{
  width: 5vw;
  text-align: center;
  position: relative;
}
.comment-user{
  color: #333;
  padding-top: 10px;
  padding-left: 0.5vw;
}
.comment-time{
  padding-left: 10px;
  position: absolute;
}
.discuss-content{
  width:68vw;
  height: auto;
  display:inline-block;
  position: relative;
  margin:10px 0px 5px 20px;
  border:1px solid #e4e4e4;
}
.discuss-info{
  width: 55vw;
  height:auto;
  margin-top: -90px;
  margin-left: 5vw;
  border-left:1px solid #e4e4e4;
}
/* 评论人 */
.commentator{
 line-height:100px;
 vertical-align:center;
}
.list-comment {
  margin-top: 15px;
  width:70vw;
}
.list-comment li {
  padding: 20px 24px;
  /* border-bottom: 1px solid #d9dde1; */
}
.list-comment-icon {
  width: 100px;
  height: auto;
}
.discuss{
  width: 60vw;
  margin: 0 auto;
}
/* 评论盒子 */
.discuss-box{
 width:70vw;
 height:auto;
 margin:0 auto;
 margin-top:30px;
}

.list-comment-main {
  width: 63vw;
  height: auto;
  margin-top: -50px;
  font-size: 0.8rem;
  color: #333;
  padding: 10px 20px 0px 10px;
  word-wrap: break-word;
}
.list-comment-foot {
  font-size: 0.6rem;
  color: #93999f;
  margin-top: 15px;
}
/* 底部分页 */
.page {
  width: 70vw;
  height: auto;
  margin: 0 auto;
}
.code-row-bg{
  padding-left: 45%;
  margin:0 auto;
}
.vjs-poster {
  width: 70vw;
  position: absolute;
  background-repeat: no-repeat;
  background-size: cover;
}
</style>
